﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class MenuSectionControl : UserControl, IControlRulable
    {
        private MenuSection innerMenuSection;

        public MenuSection InnerMenuSection
        {
            get { return innerMenuSection; }
            set { innerMenuSection = value; }
        }

        public MenuSectionControl()
        {
            InitializeComponent();

            this.ecDisplayMode.Values = Enum.GetValues(typeof(Enumerations.DisplayMode));

            this.innerMenuSection = new MenuSection();
            this.FillControls();
        }

        public MenuSectionControl(MenuSection menuSection):this()
        {
            this.innerMenuSection = menuSection;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                if (this.innerMenuSection.DisplayMode != null)
                {
                    this.ecDisplayMode.SelectedValue = this.innerMenuSection.DisplayMode.Value.ToString();
                }

                this.strId.Value = this.innerMenuSection.Id;
                this.strMaxHeight.Value = this.innerMenuSection.MaxHeight;
                this.bcScrollable.Value = this.innerMenuSection.Scrollable.Value;
                this.icSequence.Value = this.innerMenuSection.Sequence;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerMenuSection.DisplayMode = (Enumerations.DisplayMode)Enum.Parse(typeof(Enumerations.DisplayMode), this.ecDisplayMode.SelectedValue);
                    this.innerMenuSection.Id = this.strId.Value;
                    this.innerMenuSection.MaxHeight = this.strMaxHeight.Value;
                    this.innerMenuSection.Scrollable = this.bcScrollable.Value;
                    this.innerMenuSection.Sequence = this.icSequence.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MenuSectionControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerMenuSection.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
